Skip to main content

Lambda Expressions

Lambda expressions are an alternative way of writing anonymous functions.

Lua Way
local s1 = "123"
local s2 = s1:gsub(".", function(c) return tonumber(c) + 1 end)
print(s2) -- "234"
Pluto Way
local s1 = "123"
local s2 = s1:gsub(".", |c| -> tonumber(c) + 1)
print(s2) -- "234"

Try It Yourself

As you can see, they take an expression after the arrow, the result of which is implicitly returned.

However, lambda expressions can also have full bodies using do...end:

local add = |a, b| -> do
print($"Adding {a} and {b} together")
return a + b
end

They also support all other syntax you would expect of functions, such as type hints and default arguments.